home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d3 / rettig.arc / TRSOURCE.EXE / WPSTRIP.C < prev    next >
C/C++ Source or Header  |  1990-10-22  |  2KB  |  81 lines

  1. /*********
  2. *
  3. * WPSTRIP.C
  4. *
  5. * by Tom Rettig
  6. *
  7. * Placed in the public domain by Tom Rettig Associates, 10/22/1990.
  8. *
  9. *  Syntax: WPSTRIP( <expC> [,<expN>] )
  10. *  Return: <expC> with all occurrences of newline CHR(10),
  11. *             carriage-return CHR(13), and soft carriage-return
  12. *             CHR(141) removed.  Tab-characters are replaced with
  13. *             <expN> blank spaces (default 1 if omitted).
  14. *             Ascii values over 127 (except 141) are unchanged.
  15. *          Error message if out of memory.
  16. *  Note  : Maximum <expN> is 16, minimum is zero; no error returned.
  17. *********/
  18.  
  19. #include "trlib.h"
  20.  
  21. TRTYPE wpstrip()
  22. {
  23.    static char funcname[] = { "wpstrip" };
  24.    char *instr, *ret;
  25.    int i, j, k, tabs, spaces, was_space;
  26.  
  27.    if ( (PCOUNT==2 && ISCHAR(1) && ISNUM(2)) ||
  28.         (PCOUNT==1 && ISCHAR(1))              )
  29.    {
  30.       instr = _parc(1);
  31.       if ( PCOUNT==1 )
  32.       {
  33.          spaces = 1;
  34.          tabs = 0;
  35.       }
  36.       else
  37.       {
  38.          spaces = _parni(2) % MAXEXPAND;
  39.          if ( spaces < 0 )
  40.             spaces = 0;
  41.          for ( i=tabs=0; instr[i]; i++ )    /* count tabs */
  42.             tabs += instr[i]=='\t';
  43.       }
  44.       ret = _tr_allocmem( (unsigned)(_tr_strlen(instr)+1+(tabs*(spaces-1))));
  45.  
  46.       if ( ret )
  47.       {
  48.          for ( i=0, j=0; instr[j]; j++)
  49.          {
  50.             switch ( instr[j] )
  51.             {
  52.                case ( '\t' ):
  53.                   for ( k=1; k<=spaces; k++ )
  54.                      ret[i++] = SPACEC;
  55.                   was_space = TRUE;
  56.                   break;
  57.                case ( '\r' ):
  58.                case ( '\x8d' ):        /* 'soft' carriage return */
  59.                   if ( !was_space )
  60.                      ret[i++] = SPACEC;
  61.                   /* was_space not set so as to put a space for each cr */
  62.                   break;
  63.                case ( '\n' ):
  64.                   break;
  65.                default:
  66.                   ret[i++] = instr[j];
  67.                   was_space = ( instr[j] == SPACEC );
  68.             }
  69.          }
  70.          ret[i] = NULLC;
  71.  
  72.          _retc( ret );
  73.          _tr_freemem( ret,(unsigned)(_tr_strlen(instr)+1+(tabs*(spaces-1))));
  74.       }
  75.       else
  76.          _retc( _tr_errmsgs(funcname,E_ALLOC) );
  77.    }
  78.    else
  79.       _retc( _tr_errmsgs(funcname,E_SYNTAX) );
  80. }
  81.